#ifndef QFORMWIDGET_H
#define QFORMWIDGET_H

#include <QWidget>
#include <QRubberBand>

class Selection;

class WidgetHost;
class QPropertyListView;
class AbstractCore;
class QObjectHost;

class QFormWidget : public QWidget
{
    Q_OBJECT
public:
    explicit QFormWidget(AbstractCore *core,QWidget *parent = 0);

    void    setWidgetHost(WidgetHost* host);
    WidgetHost* widgetHost();
    void    setListView(QPropertyListView* listview);

    QString saveBuffer();
    bool    load(const QString &buffer);

    bool    dirty();
    void    setDirty(bool dirty);

    QString name();
    void    setName(const QString& name);

    void    selectThis();

    void    alignLeft();
    void    alignRight();
    void    alignTop();
    void    alignBottom();

    void    alignHCenter();
    void    alignVCenter();

    QWidgetList widgets();
    void    select(QWidget *wid);
protected:
    bool    handleMousePressEvent(QWidget* wid, QMouseEvent *e);
    bool    handleMouseMoveEvent(QWidget* wid, QMouseEvent *e);
    bool    handleMouseRelessEvent(QWidget* wid, QMouseEvent *e);
    bool    handleMouseDoubleEvent(QWidget* wid, QMouseEvent *e);
    bool    handlePainteEvent(QWidget* wid,QPaintEvent *e);

protected:
    void paintEvent(QPaintEvent *);
    bool eventFilter(QObject *, QEvent *);

    void mousePressEvent(QMouseEvent *);
    void mouseMoveEvent(QMouseEvent *);
    void mouseReleaseEvent(QMouseEvent *);
    void keyPressEvent(QKeyEvent *);

    void dropEvent(QDropEvent *);
    void dragEnterEvent(QDragEnterEvent *);

    void selectWidget(QWidget* wid);
    void deleteSelectWidget();

    void resizeEvent(QResizeEvent *);

public:
protected:
    QWidget *designerWidget(QWidget *widget);

    QWidget* addWidget(QString name,QString ui);

    void    movePress(int type);

    QString getObjectName(QWidget* wid);
protected slots:
    void    formChanged();
    void    objectNameChanged();
    void    updateZValue();
    void    updatePropertyAttribute();
signals:
    void    changed();
    void    currentChanged(QWidget* widget);

    void    widgetAdd(QWidget*  wid);
    void    widgetRemove(QWidget* wid);
    void    nameChanged(QWidget* wid);
protected:
    QRubberBand     *m_rubber;
    QPoint          m_clickPoint;
    Selection       *m_selection;
    QList<QWidget*> m_widgets;
    WidgetHost      *m_widgetHost;
    QPropertyListView   *m_listview;
    AbstractCore    *m_core;
    bool            m_click;
    bool            m_dirty;
    QString         m_name;
    QObjectHost     *m_host;
};

#endif // QFORMWIDGET_H
